package znbkListSc.adapter;

import android.animation.Animator;
import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.example.lancooznbk.R;
import znbkListSc.entity.CollectionListEntity;
import com.example.lancooznbk.utils.AnimatorHelper;
import com.example.lancooznbk.view.widget.RippleView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\2\10 0010.
 */

public class CollectionListAdapter extends BaseRecyclerAdapter<CollectionListAdapter.CollectionAdapterViewHolder> {

    private List<CollectionListEntity.FavouriteBean> list = new ArrayList<>();
    private final Activity context;
    private OnItemClickListener onItemClickListener;

    private int actionItem;

    private final Interpolator mInterpolator = new LinearInterpolator();
    private int mLastPosition = -1;


    public interface OnItemClickListener{
        void onItemClick(View view, int position);
        void onItemDel(View view, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public CollectionListAdapter(Activity context, List<CollectionListEntity.FavouriteBean> favouriteBeanList) {
        this.context = context;
        this.list = favouriteBeanList;
    }

    public CollectionListAdapter(Activity context) {
        this.context = context;
    }


    @Override
    public void onBindViewHolder(final CollectionAdapterViewHolder holder, final int position, boolean isItem) {

        holder.favouriteItem.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() {
            @Override
            public void onComplete(RippleView v) {

                if (onItemClickListener != null && list.size() != 0) {//修复数据清除了界面还存在点击的bug
                    actionItem = (Integer) holder.favouriteItem.getTag();
                    onItemClickListener.onItemClick(v,actionItem);
                }
            }
        });
        holder.btnDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null && list.size() != 0) {
                    actionItem = (Integer) holder.favouriteItem.getTag();
                    onItemClickListener.onItemDel(holder.favouriteItem,actionItem);
                }
            }
        });
        holder.favouriteItem.setTag(position);


        CollectionListEntity.FavouriteBean treeInfoEntity = list.get(position);

        holder.treeItemSummary.setText(Html.fromHtml(treeInfoEntity.getTheme()));

        String strTime = treeInfoEntity.getCreateTime();
        strTime = strTime.replaceAll("-",".");
        String strCollectionTime = String.format(context.getString(R.string.sc_item_collection_time),strTime);
        holder.favouriteTime.setText(Html.fromHtml(strCollectionTime));

        /*
        * 显示列表试题封面图标颜色
        * */
        if(treeInfoEntity.getResType().equals("1")||treeInfoEntity.getResType().equals("2")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_blue);
            holder.circleImage.setText("图文");
        }else if(treeInfoEntity.getResType().equals("3")||treeInfoEntity.getResType().equals("4")||
                treeInfoEntity.getResType().equals("5")||treeInfoEntity.getResType().equals("6")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_green);
            holder.circleImage.setText("音视频");
        }else{
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_yellow);
            holder.circleImage.setText("试题");
        }

        if(treeInfoEntity.getResType().equals("1")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_blue);
            holder.circleImage.setText("文本");
        }else if(treeInfoEntity.getResType().equals("2")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_blue);
            holder.circleImage.setText("图片");
        }else if(treeInfoEntity.getResType().equals("3")||
                  treeInfoEntity.getResType().equals("5")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_green);
            holder.circleImage.setText("声文");
        }else if(treeInfoEntity.getResType().equals("4")||
                treeInfoEntity.getResType().equals("6")){
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_green);
            holder.circleImage.setText("视文");
        }else{
            holder.circleImage.setBackgroundResource(R.mipmap.znbk_cover_ques_type_yellow);

            String itemTypeName = "";
            if(!treeInfoEntity.getGenreName().equals("")){
                itemTypeName = treeInfoEntity.getGenreName();
            }else{
                itemTypeName = treeInfoEntity.getTypeName();
            }
           itemTypeName = itemTypeName.substring(0,
                    itemTypeName.length() > 4 ? 4 : itemTypeName.length() );
            StringBuilder itemTypeBuffer = new StringBuilder(itemTypeName);
            if (itemTypeBuffer.length() > 2) {
                itemTypeBuffer.insert(2, "\n");
            }
            holder.circleImage.setText(itemTypeBuffer);

        }

        int adapterPosition = holder.getAdapterPosition();

        boolean isScrollDown = adapterPosition > mLastPosition;

        for (Animator anim : AnimatorHelper.getRecyclerAnimator(holder.itemView,isScrollDown)) {
            anim.start();
        }

    }





    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    public List<CollectionListEntity.FavouriteBean>getAdapterItemList(){
        return list;
    }
    @Override
    public CollectionAdapterViewHolder getViewHolder(View view) {
        return new CollectionAdapterViewHolder(view, false);
    }

    @Override
    public CollectionAdapterViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v;
        v = LayoutInflater.from(context).inflate(R.layout.item_rv_sc,parent,false);
        return new CollectionAdapterViewHolder(v,true);
    }

    class CollectionAdapterViewHolder extends RecyclerView.ViewHolder{
        TextView favouriteTime;
        RippleView favouriteItem;
        TextView circleImage;
        TextView treeItemSummary;

        Button btnDel;
        public CollectionAdapterViewHolder(View itemView,boolean isItem) {
            super(itemView);
            if (isItem) {
                favouriteItem = (RippleView) itemView.findViewById(R.id.ripple_favourite_item);
                circleImage = (TextView) itemView.findViewById(R.id.img_favourite_title_summary);
                treeItemSummary = (TextView) itemView.findViewById(R.id.tv_favourite_summary);
                favouriteTime = (TextView) itemView.findViewById(R.id.tv_favourite_time);
                btnDel = (Button) itemView.findViewById(R.id.btnDelete);
            }

        }


    }



    //添加数据
    public void add(List<CollectionListEntity.FavouriteBean> favouriteBeanList){
        int startPosition = getStart()+getAdapterItemCount();
        list.addAll(favouriteBeanList);
        notifyItemRangeInserted(startPosition, favouriteBeanList.size());
    }

    public void clearAndAdd(List<CollectionListEntity.FavouriteBean> favouriteBeanList){
        list.clear();
        list.addAll(favouriteBeanList);
        notifyDataSetChanged();
        mLastPosition = -1;
    }




    public void remove(int position) {
        list.remove(position);
        remove(list, position);
    }

    public void remove(){
        list.remove(actionItem);
        //notifyDataSetChanged();
        notifyItemRemoved(actionItem);
        notifyItemRangeChanged(actionItem,getItemCount());


    }

    public void clear() {
        list.clear();//清空不刷新
    }


}